local BuffBase = import(".BuffBase")
local BuffComposite = class("BuffComposite", BuffBase)
local FightConst = require("const.FightConst")

function BuffComposite:ctor(entity, whoEntity, config)
    BuffComposite.super.ctor(self, entity, whoEntity, config)
    self._children = {}
    self._removeCaches = {}
end

function BuffComposite:addChild(child)
    table.insert(self._children, child)
    child:setParent(self)
    child:awake()
end

function BuffComposite:removeChild(child)
    table.insert(self._removeCaches, child)
end

function BuffComposite:getChildCount()
    return #self._children
end

function BuffComposite:_clearRemoveCaches()
    for i = 1, #self._removeCaches do
        self:_removeChild(self._removeCaches[i])
    end
    self._removeCaches = {}
    if #self._children == 0 then
        self:remove()
    end
end

function BuffComposite:_removeChild(child)
    for i = 1, #self._children do
        if child == self._children[i] then
            table.remove(self._children, i)
            break
        end
    end
end

function BuffComposite:check()
    self._params = nil
    for i = 1, #self._children do
        self._children[i]:check()
    end
    if self._parent then
        self._parent:collectParams(self._params)
    else
        self:_trigger()
    end
    self:_clearRemoveCaches()
end

function BuffComposite:_trigger()
    if not self._params then
        return
    end
    self:_pause()
    self:_optAttr()
end

function BuffComposite:_optAttrEx(id, value)
    self._entity:subAttrValue(id, value)
end

function BuffComposite:_optAttr()
    if self._params.typeOpt == "add" then
        self:_optAttrEx(self._params.attrId, -self._params.value)
        G_SignalManager:dispatch(
            SignalConst.EVENT_FIGHT_HURT_TEXT,
            {parent = self._entity:getBloodUI(), text = self._params.value, element = AttributeConst.ELEMENT_TREATMENT}
        )
    else
        local AttributeConst = require("const.AttributeConst")
        if self._params.attrId == AttributeConst.ATTACK then
            local atkAttr = require("gameLogic.fightEntity.Attributes").new()
            atkAttr:setAttrValue(self._params.attrId, self._params.value)
            local hurt = StrategyHelper.simpleHurt(atkAttr, self._entity:getAttr(), 1)
            self:_optAttrEx(AttributeConst.BLOOD, hurt)
        else
            self:_optAttrEx(self._params.attrId, self._params.value)
        end
        G_SignalManager:dispatch(
            SignalConst.EVENT_FIGHT_HURT_TEXT,
            {parent = self._entity:getBloodUI(), text = self._params.value, element = AttributeConst.ELEMENT_PHYSICS}
        )
    end
end

function BuffComposite:_pause()
    local pauseTatrget = nil
    if self:getType() == FightConst.BUFF_ACTION_BEGIN then
        pauseTatrget = self._entity
    else
        pauseTatrget = self._entity:getManager()
    end
    pauseTatrget:setPause(true)
    local sequence = DOTween.Sequence()
    sequence:AppendInterval(0.8)
    sequence:AppendCallback(
        function()
            pauseTatrget:setPause(false)
        end
    )
end

-- 收集参数
function BuffComposite:collectParams(params)
    if not params then
        return
    end
    if not self._params then
        self._params = {}
    end
    self._params.typeOpt = params.typeOpt
    self._params.attrId = params.attrId
    if self._params.value then
        self._params.value = self._params.value + params.value
    else
        self._params.value = params.value
    end
end

-- 无参数解析置空
function BuffComposite:parseParams(params)
end

function BuffComposite:onDestroy()
    self._children = {}
    self._removeCaches = {}
end

return BuffComposite
